﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SaleManagement.Web.App_Code;
using DevExpress.Web.ASPxGridView;
using SaleManagement.Services;
using SaleManagement.Domains;

namespace SaleManagement.Web.Pages
{
  public partial class ProductCategory : BasePage
  {
    #region Properties
    private string m_innerError = string.Empty;

    #endregion

    #region Method
    private void LoadPage()
    {

    }

    private void LoadProductCategory()
    {
      gvProductGroup.DataSource = new ProductGroupService().GetAll();
      gvProductGroup.DataBind();
    }

    private string SaveProductGroup()
    {
      try
      {
        ProductGroupService proGroupService = new ProductGroupService();
        Domains.ProductGroup pro = new Domains.ProductGroup();

        if (!string.IsNullOrEmpty(hfProductGroupId.Value))
        {
          int groupId = -1;
          int.TryParse(hfProductGroupId.Value, out groupId);
          pro = proGroupService.GetById(groupId);
          if (pro == null) return "Save|Không tìm thấy nhóm hàng hóa tương ứng";
        }
        else
        {
          pro = new ProductGroup();
        }
        pro.Name = txtProductGroupName.Text;
        pro.IsInventory = cbxInventory.Checked;
        proGroupService.Save(pro);
        return "Save|";

      }
      catch (Exception ex)
      {
        WriteLog("ProductCategory.aspx :: Save Product Category", ex);
        return "Save|Có lỗi trong quá trình lưu dữ liệu";
      }

    }
    private string DeleteProductGroup(string groupId)
    {
      try
      {
        int id = int.Parse(groupId);
        ProductGroupService proGroupService = new ProductGroupService();
        Domains.ProductGroup pro = new Domains.ProductGroup();
        pro = proGroupService.GetById(id);
        TList<Product> productList = new ProductService().Find("GroupId=" + id);
        if (productList.Count > 0) return "Delete|Không thể xóa do còn hàng hóa thuộc về nhóm này";
        proGroupService.Delete(id);
        return "Delete|";
      }
      catch (Exception ex)
      {
        WriteLog("ProductCategory.aspx :: Delete Product Category", ex);
        return "Delete|Có lỗi trong quá trình lưu dữ liệu";
      }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadPage();
        LoadProductCategory();
      }
    }

    protected void gvProductGroup_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Save")
      {
       // int productId = int.Parse(para[1]);
        m_innerError = SaveProductGroup();
      }
      else if (para[0] == "Delete")
      {
        m_innerError = DeleteProductGroup(para[1]);
      }
      LoadProductCategory();
    }
    protected void gvProductGroup_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }
    protected void gvProductGroup_PageIndexChanged(object sender, EventArgs e)
    {
      LoadProductCategory();
    }
  }
}
